import Vue from 'vue';
import locale from '@/config/locale';
import tokenSvc from '@/services/token';
import storage from '@/utils/storage';
import { downloadFiles } from '@csii/vx-util';
import strToJson from '@/utils/strToJson';

Vue.mixin({
  data() {
    return {
      headerTitle: undefined, // 上面span里面的title
      titleArray: [], // 上面面包屑的数组
    };
  },
  mounted() {
    this.getHeaderTitle();
  },
  methods: {
    ...storage,
    ...strToJson,
    downloadFiles,
    // 相当于定义全局方法
    $field: function(key) {
      return locale.FIELDS[key];
    },
    $msg: function(key) {
      return locale.MESSAGES[key];
    },

    // 获得防重复提交token
    getToken: function(apiKey) {
      return tokenSvc.refreshToken(apiKey);
    },
    // 统一查询方法得到当前位置数据的数组
    getHeaderTitle: function() {
      if (this.$store && this.$store.state.vxadmin.menu.currentPosition && this.$store.state.vxadmin.menu.currentPosition.length > 0) {
        this.titleArray = this.$store.state.vxadmin.menu.currentPosition;
        this.headerTitle = this.titleArray[this.titleArray.length - 1].title;
      }
    },
  },
});
